package org.fixies.spreadsheet.converter;

import org.dbunit.dataset.IDataSet;
import org.fixies.database.dbunit.DataSetImpl;
import org.fixies.spreadsheet.Unit;

import java.util.ArrayList;
import java.util.List;


public class IDataSetConverter extends AbstractSheetConverter<IDataSet> {

    @Override
    public IDataSet convert() {
        List<Unit> data = new ArrayList<Unit>();
        for (Unit unit : getSheet().getUnits()) {
            if (isTarget(unit)) {
                data.add(unit);
            }
        }
        return new DataSetImpl(data);
    }

    private boolean isTarget(Unit unit) {

        String type = unit.getMetaData("TYPE");
        if (!"TABLE".equals(type)) {
            return false;
        }

        String prepare = unit.getMetaData("PREPARE");
        return prepare != null && prepare.equals("TRUE");
    }

}
